import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.HashMap;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Stub;

import com.liferay.portal.model.PortletPreferencesIds;
import com.liferay.portal.model.UserSoap;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.http.UserServiceSoap;
import com.liferay.portal.service.http.UserServiceSoapServiceLocator;

/**
 * 
 * http://www.liferay.com/web/antonio.junior/blog/-/blogs/12168124
 * 
 * 
 */
public class LFRWSClient {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        long companyId = Long.parseLong(args[0]);
        String emailAddress = args[1];
        String username = args[2];
        String password = args[3];

        URL endpointURL = new URL(
                "http://127.0.0.1:8080/api/secure/axis/Portal_UserService");
        new LFRWSClient().execute(endpointURL, companyId, emailAddress,
                username, password);
    }

    private void execute(URL endpointURL, long companyId, String emailAddress,
            String username, String password) throws RemoteException,
            ServiceException {

        
        UserServiceSoap service = getService(endpointURL, username, password);
//      emailAddress = "temp@example.com";
//        UserSoap user = service.getUserByEmailAddress(companyId,
//        emailAddress);
//        System.out.println(user.getStatus());
//        System.out.println(user.getUserId());
        
        //addUser(service);
        //deleteUser(service);
        //updateUser(service);
    }

    private void deleteUser(UserServiceSoap service) throws RemoteException {
        service.deleteUser(13001);
    }

    private void updateUser(UserServiceSoap service) throws RemoteException {
        int status = 0; // active
        //int status = 5; // inactive
        // int status = 2; // initial state
        service.updateStatus(13019, status);
    }

    
    private void addUser(UserServiceSoap service) throws RemoteException {

        long companyId = 1;
        boolean autoPassword = false;
        String password1 = "password";
        String password2 = "password";
        String emailAddress = "temp@example.com";
        long facebookId = 0;
        String openId = "";
        boolean autoScreenName = true;
        String screenName = "";
        String locale = "ja_JP";
        String firstName = "Temp";
        String middleName = "";
        String lastName = "Piyo";
        int prefixId = 0;
        int suffixId = 0;
        String jobTitle = "example job title";
        int birthdayYear = 2001;
        int birthdayDay = 1;
        int birthdayMonth = 4;
        boolean male = true;
        long[] groupIds = new long[0];
        long[] organizationIds = new long[0];
        long[] roleIds = new long[0];
        long[] userGroupIds = new long[0];
        boolean sendEmail = false;
        ServiceContext serviceContext = new ServiceContext();

        service.addUser(companyId, autoPassword, password1, password2,
                autoScreenName, screenName, emailAddress, facebookId, openId,
                locale, firstName, middleName, lastName, prefixId, suffixId,
                male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
                groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
                serviceContext);

    }

    private UserServiceSoap getService(URL endpointURL, String username,
            String password) throws ServiceException {
        UserServiceSoapServiceLocator locator = new UserServiceSoapServiceLocator();
        UserServiceSoap service = locator.getPortal_UserService(endpointURL);
        ((Stub) service).setMaintainSession(true);
        ((Stub) service).setUsername(username);
        ((Stub) service).setPassword(password);
        return service;
    }

}
